﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

public class TransparentTextPanel : Panel
{
    private FlowLayoutPanel flowPanel;
    private VScrollBar vScrollBar;
    private int scrollPosition = 0;
    private Color panelBackColor = Color.FromArgb(0, 217, 217, 217); // 半透明背景色

    public TransparentTextPanel()
    {
        // 主面板设置
        this.BackColor = Color.Transparent;
        this.DoubleBuffered = true;
        this.Padding = new Padding(0, 0, 15, 0); // 为滚动条留出空间

        // 滚动条设置
        vScrollBar = new VScrollBar();
        vScrollBar.Dock = DockStyle.Right;
        vScrollBar.Width = 15;
        vScrollBar.Scroll += (s, e) =>
        {
            scrollPosition = vScrollBar.Value;
            flowPanel.Top = -scrollPosition;
        };
        this.Controls.Add(vScrollBar);

        // 内容流式面板
        flowPanel = new FlowLayoutPanel();
        flowPanel.BackColor = panelBackColor;
        flowPanel.AutoSize = true;
        flowPanel.WrapContents = false;
        flowPanel.FlowDirection = FlowDirection.TopDown;
        flowPanel.Left = 0;
        flowPanel.Top = 0;
        flowPanel.Width = this.ClientSize.Width - vScrollBar.Width;
        this.Controls.Add(flowPanel);

        // 初始时隐藏滚动条
        vScrollBar.Visible = false;
    }

    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        UpdateScrollBar();
        flowPanel.Width = this.ClientSize.Width - (vScrollBar.Visible ? vScrollBar.Width : 0);
    }

    private void UpdateScrollBar()
    {
        // 计算是否需要显示滚动条
        bool needScroll = flowPanel.Height > this.ClientSize.Height;
        vScrollBar.Visible = needScroll;

        if (needScroll)
        {
            // 设置滚动条范围
            vScrollBar.Minimum = 0;
            vScrollBar.Maximum = flowPanel.Height - this.ClientSize.Height;
            vScrollBar.LargeChange = Math.Max(10, this.ClientSize.Height / 2);
            vScrollBar.SmallChange = 10;

            // 确保当前滚动位置在有效范围内
            scrollPosition = Math.Min(scrollPosition, vScrollBar.Maximum);
            scrollPosition = Math.Max(scrollPosition, vScrollBar.Minimum);

            vScrollBar.Value = scrollPosition;
            flowPanel.Top = -scrollPosition;
        }
        else
        {
            flowPanel.Top = 0;
            scrollPosition = 0;
        }

        // 调整flowPanel宽度，考虑滚动条是否可见
        flowPanel.Width = this.ClientSize.Width - (vScrollBar.Visible ? vScrollBar.Width : 0);
    }

    // 添加文本段落
    public void AppendText(string text, Color color)
    {
        Label lbl = new Label();
        lbl.Text = text;
        lbl.Font = new Font("宋体", 9F);
        lbl.ForeColor = color;
        lbl.BackColor = Color.Transparent;
        lbl.AutoSize = false;  // 必须设置为false才能换行
        lbl.Width = flowPanel.Width - 15 - lbl.Margin.Left - lbl.Margin.Right; // 设置宽度
        lbl.MaximumSize = new Size(lbl.Width, 0); // 高度设为0表示不限制
        lbl.Margin = new Padding(5, 2, 5, 2);

        // 计算文本所需高度
        using (Graphics g = CreateGraphics())
        {
            SizeF size = g.MeasureString(text, lbl.Font, lbl.Width);
            lbl.Height = (int)Math.Ceiling(size.Height);
        }

        flowPanel.Controls.Add(lbl);
        UpdateScrollBar();
    }

    // 设置背景透明度 (0-255)
    public void SetBackgroundTransparency()
    {
        panelBackColor = Color.FromArgb(80, 217, 217, 217);
        flowPanel.BackColor = panelBackColor;
        this.Invalidate();
    }

    // 清空内容
    public void Clear()
    {
        flowPanel.Controls.Clear();
        UpdateScrollBar();
    }

    // 自动滚动到底部
    public void ScrollToBottom()
    {
        if (vScrollBar.Visible)
        {
            vScrollBar.Value = vScrollBar.Maximum;
            flowPanel.Top = -vScrollBar.Value;
            scrollPosition = vScrollBar.Value;
        }
    }

    // 支持鼠标滚轮滚动
    protected override void OnMouseWheel(MouseEventArgs e)
    {
        if (vScrollBar.Visible)
        {
            int newValue = vScrollBar.Value - e.Delta;
            vScrollBar.Value = Math.Max(vScrollBar.Minimum, Math.Min(vScrollBar.Maximum, newValue));
            flowPanel.Top = -vScrollBar.Value;
            scrollPosition = vScrollBar.Value;
        }
        base.OnMouseWheel(e);
    }
}